﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTelluro.Explorer.Domain.MainBoundedContext;
using iTelluro.Explorer.Domain.MainBoundedContext.Entities;
using iTelluro.Explorer.Application.MainBoundContext.DTO;
using iTelluro.Explorer.Application.Seedwork;
using iTelluro.Explorer.Domain.Seedwork;
using iTelluro.Explorer.Domain.Seedwork.Specification;

namespace iTelluro.Explorer.Application.MainBoundContext
{
    public class meteorologicalAppService:ImeteorologicalAppService
    {
        private IMetalertRepository _repository;

        public meteorologicalAppService(IMetalertRepository repository)
        {
            _repository = repository;
            
        }
        /// <summary>
        /// 新增一条Metalert记录
        /// </summary>
        /// <param name="dto">Metalert实体</param>
        /// <returns>是否新增成功</returns>
        public bool Add(MetalertDTO dto)
        {
            try
            {
                var entity = dto.ProjectedAs<MetAlert>();
                _repository.Add(entity);
                _repository.UniteOfWork.Commit();
                return true;
               
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 根据主键删除一条Metalert记录
        /// </summary>
        /// <param name="guid">Metalert主键值</param>
        /// <returns>是否删除成功</returns>
        public bool Remove(string guid)
        {
            try
            {
                var dicDataRepository = _repository.Get(guid);
                if (dicDataRepository != null)
                {
                    _repository.Remove(dicDataRepository);
                    _repository.UniteOfWork.Commit();
                  

                }
                return true;
                             
            }
            catch (Exception ex)
            {
                throw ex;
                
               
            }
        }

        /// <summary>
        /// 更新一条Metalert记录
        /// </summary>
        /// <param name="dto">Metalert实体</param>
        /// <returns>是否更新成功</returns>
        public bool Update(MetalertDTO dto)
        {
            try
            {
              
                var persist = _repository.Get(dto.ID);
                var entities = dto.ProjectedAs<MetalertDTO>();
                if(persist!=null)
                {
                    _repository.UniteOfWork.Commit();
                }
                return true;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 根据Metalert主键获取一条记录
        /// </summary>
        /// <param name="guid">Metalert主键值</param>
        /// <returns>根据主键查询到的Metalert记录</returns>
        public MetalertDTO Get(string guid)
        {
            try
            {
                var entity = _repository.Get(guid);
                return entity.ProjectedAs<MetalertDTO>();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 查询所有Metalert记录
        /// </summary>
        /// <returns>Metalert所有记录集合</returns>
        public List<MetalertDTO> GetAll()
        {
            try
            {
                return _repository.GetAll().ProjectedAsCollection<MetalertDTO>();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<MetalertDTO> FindWarningData(string Issuetime)
        {
            string sd = Issuetime.Remove(8, 6);
            List<MetalertDTO> entities = new List<MetalertDTO>();
            Specification<MetAlert> spe = new TrueSpecification<MetAlert>();
            if (Issuetime != null)
            {
                spe &= new DirectSpecification<MetAlert>(s => s.Issuetime.StartsWith(sd));
            }

            return _repository.GetFiltered(spe.SatisfiedBy()).ProjectedAsCollection<MetalertDTO>();

        }
    }
}
